#!/bin/bash

#Change the build command to:
#erlang.sublime-build
#{
#	"cmd": ["cmd","/C","cd .. & make.bat"],
#	"file_regex":"^([^:]+):([0-9]*):?(.*):?(.*)",
#	"selector": "source.erl"
#}

function usage() {
    echo "usage: create_project <projectname>"
    exit 1
}

PROJECT_NAME=$1
PROJECT_DIR=./$PROJECT_NAME

if [ -z  $PROJECT_NAME ] 
then
	usage
fi

echo -n "Generating project structure... "

mkdir $PROJECT_DIR
mkdir $PROJECT_DIR/config
mkdir $PROJECT_DIR/src
mkdir $PROJECT_DIR/ebin
mkdir $PROJECT_DIR/include
mkdir $PROJECT_DIR/logs

echo "@echo off" > $PROJECT_DIR/start.bat
echo "cd ebin" >> $PROJECT_DIR/start.bat
echo "werl %1 %2" >> $PROJECT_DIR/start.bat

echo "@echo off" > $PROJECT_DIR/make.bat
echo "if not exist ebin mkdir ebin" >> $PROJECT_DIR/make.bat
echo "erl -make" >> $PROJECT_DIR/make.bat

echo "#!/bin/bash" > $PROJECT_DIR/make.sh
echo "mkdir -p ebin" >> $PROJECT_DIR/make.sh
echo "erl -make" >> $PROJECT_DIR/make.sh

echo "{" > $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '	"folders":' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '	[' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		{' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '			"path": "include"' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		},' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		{' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '			"path": "config"' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		},' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		{' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '			"path": "src"' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '		}' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '	]' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project
echo '}' >> $PROJECT_DIR/$PROJECT_NAME.sublime-project

echo "{'src/*', [debug_info,"         > $PROJECT_DIR/Emakefile
echo '           {i, "src"},'               >> $PROJECT_DIR/Emakefile
echo '           {i, "include"},'         >> $PROJECT_DIR/Emakefile
echo '           {outdir, "ebin"}]}.'   >> $PROJECT_DIR/Emakefile

